package library.web.servlet.ajax;

import com.alibaba.fastjson2.JSON;
import library.domain.Book;
import library.persistence.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class EditBook extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int type = Integer.parseInt(req.getParameter("type")) ;
//        System.out.println(type);

        BookService bookService = new BookService();
        if(type==1){ //刷新页面，显示所有图书
            List<Book> bookList = bookService.getAllBook();
            String jsonString = JSON.toJSONString(bookList);
            resp.setContentType("text/json;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            out.print(jsonString);
        }else if(type==2){ //向数据库中增加图书
            int bookId = Integer.parseInt(req.getParameter("bookId"));
            String bookName = req.getParameter("bookName");
            String bookAuthor = req.getParameter("bookAuthor");
            String bookClass = req.getParameter("bookClass");
            String bookPress = req.getParameter("bookPress");
            String bookDescription = req.getParameter("bookDescription");
            int bookInventory = Integer.parseInt(req.getParameter("bookInventory"));
            Book book = new Book(bookId,bookName,bookAuthor,bookClass,bookPress,bookDescription,bookInventory);
            bookService.insertBook(book);
            PrintWriter out = resp.getWriter();
            out.print("Success");
        }else if(type == 3){  //向数据库中筛选书籍
            String keyword = req.getParameter("keyword");
            List<Book> bookList = bookService.searchBookList(keyword);
            String jsonString = JSON.toJSONString(bookList);
            resp.setContentType("text/json;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            out.print(jsonString);
        }else if(type == 4){  //点击搜索按钮筛选包含该keyword的所有书籍
            String liBookName = req.getParameter("liBookName");
            Book book = bookService.getBookByBookName(liBookName);
            String jsonString = JSON.toJSONString(book);
            resp.setContentType("text/json;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            out.print(jsonString);
        }else if(type == 5){   //在数据库中删除该图书
            int deleteBookId = Integer.parseInt(req.getParameter("deleteBookId"));
            bookService.deleteBookByBookId(deleteBookId);
            PrintWriter out = resp.getWriter();
            out.print("Success");
        }

    }

//    public static void main(String[] args) {
//        List<Book> bookList = new BookService().getAllBook();
//        String jsonString = JSON.toJSONString(bookList);
//
//        System.out.println(jsonString);
//    }

}
